<%@ page contentType="text/html; charset=utf-8" %>

<%@page import="org.apache.commons.lang.*"%>
<%--
SimManager uses Form based authentication and this is the login form page.
It is defined in web.xml file for <form-login-page>.
The username and password are defined in the tomcat-users.xml file located in conf folder of tomcat server.
If the username/password entered is incorrect will call error_sso.jsp, but if entered correct will call index.jsp.
--%>
<html>
<head>
<title>MaterialWorX Login Page</title>
<script type="text/javascript" src="../js/jquery/js/jquery-1.11.2.min.js"></script>
</head>
 	
 	
 	<%
 		String cxtPath = "http://" + request.getLocalAddr() + ":8989/MaterialWorX" ;
 		String localAddr = request.getLocalAddr();
 		if( "10.112.10.121".equals( localAddr ) || "10.112.10.122".equals( localAddr ) ){
 			 cxtPath = "https://matworx.nioint.com/MaterialWorX" ;
 		}
 	
 		String uname = request.getParameter( "u" );
		String pwd   = request.getParameter( "p" );
		String uid   = request.getParameter( "uid" );
		String tn    = request.getParameter( "tn" );
		
		if( StringUtils.isNotBlank( uname ) && StringUtils.isNotBlank( pwd )  && StringUtils.isNotBlank( uid )  ){
		
			String loginUser = ( String )session.getAttribute( "username" );
			if( "CommonUser".equals( loginUser ) || uname.equals( loginUser ) ){
				//redirect sod 
				com.msc.sdm.ic.pub.SimMgrClientConnection conn = com.msc.sdm.application.ExecutionContext.getInstance().getClientConnection();
				//String typeId = conn.lookupObject( "ObjectType", "[name=='NIOMaterial']" ).getId();
				
				String oid = "";
				if( StringUtils.isBlank( tn ) ){
					com.msc.sdm.ic.pub.SimMgrDbObject dbObject = conn.search( "NIOMaterial" ,"[uniqueId=='"+uid+"'].sort(createdAt,'d')").get(0);
					oid = dbObject.getId() + ":" + dbObject.getObjectTypeId() ;
				}else {
					com.msc.sdm.ic.pub.SimMgrDbObjectList dbObjectList = conn.evaluate( "NIOTestData" ,"[generates:NIOMaterial.uniqueId=='"+uid+"' AND materialTradeName=='"+tn+"'].$${GlobalQuery}").getDbObjectList();
					
					for( int i = 0 ; i < dbObjectList.size() ; i++ ){
						if( i > 0 ){
							oid += "," ;
						}
						oid += dbObjectList.get(i).getId() + ":" + dbObjectList.get(i).getObjectTypeId() ;
					}
				}
				//response.sendRedirect( cxtPath + "/facelets/main/application.xhtml?query=2;" + oid + ";this");   
				response.sendRedirect( cxtPath + "/facelets/main/application.xhtml?query=3;" + oid + ";this");
			}else{
				//login
				pageContext.setAttribute( "cxtPath", cxtPath );
				pageContext.setAttribute( "uname", uname );
				request.getSession().setAttribute( "_SESSION_" , pwd );
			}
		}else{
			response.sendRedirect( cxtPath + "/facelets/logon.xhtml");   
		}
 	%>
 
	<body >
		<h3>MaterialWorX Material Display!!</h3>
	</body>
 	<script type="text/javascript">
 	  
 	 function setCookie(name,value) {
	 	var Days = 30;
	 	var exp = new Date();
	 	exp.setTime(exp.getTime() + Days*24*60*60*1000);
	 	document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
 	 }
 	
	  $(function(){
		  //setCookie("MaterialCenter_textLocale" , "zh/CN");
		  //alert( "${param.smlocale }");
		  var ctxPath = "${cxtPath}";
		  var url =  ctxPath + "/servlet/rest/login";
		  
		  $.post( url , { 'user':'${uname}','passwd':'_SESSION_','f':'ENOVIA'  } )
		  .done(function( data ) {
			  
		     var loginSuccess = $( "login[success='true']" , data ).size();
		     if( loginSuccess > 0 ){
		    	 //location.href = ctxPath +  "/facelets/main/application.xhtml?query=2;${param.oid};this";
				 location.reload();
		     }else{
		    	 location.href = ctxPath + "/facelets/logon.xhtml";
		     }
		  }).fail(function() {
		  		 location.href = ctxPath + "/facelets/logon.xhtml";
		  }); 
	  });
	</script>
</html>
